function [Dist_estim,Velo_estim,theta_est]=RX(Nsc,Ncp,Nsym,i_sto,fs,N_ISAC,...)
    t=[(Nsc+Ncp)*Nsym*(i_sto-1):1:(Nsc+Ncp)*Nsym*i_sto-1]/fs*N_ISAC;
    tau=2*(UAV.acc(2))/c;
    SampleOffset=floor(tau*fs);
H_target = zeros(Nt,Nr,length(t));
% k = -Nsc/2 : Nsc/2-1;
% f_k = fc+ScS*k;
%     delay=exp(-1j*2*pi*f_k(ns)*tau);
for i_t=1:length(t)
    doppler = exp(1j * 2 * pi * fd * t(i_t));
    At = exp(-1j * (0:Nt-1)' * pi * sind(theta_target)) / sqrt(Nt);
    Ar = exp(-1j * (0:Nr-1)' * pi * sind(theta_target)) / sqrt(Nr);
    H_target(:,:,i_t) = Ar * doppler * At';
end
for i_t=1:length(t)
    RX_target(:,i_t)=H_target(:,:,i_t)*TX(:,i_t);
end
%RX_target=awgn(RX_target,SNR);
for ant=1:Nr
    tmp=[zeros(1,SampleOffset),squeeze(RX_target(ant,:))];
    RX_target(ant,:)=tmp(1:length(RX_target));
end

RX=RX_target+RX_tmp;

RX_FD_tmp=zeros(Nsc+Ncp,Nsym,Nr);   %存储接收数据
m=1;
for i_sym=1:Nsym
    for i_nsc = 1:Nsc+Ncp
        RX_FD_tmp(i_nsc,i_sym,:)=RX(:,m);    
        m=m+1;
    end
end
RX_FD_tmp = RX_FD_tmp(Ncp+1:Ncp+Nsc,:,:);     % 去CP&维度[Nsc Nsym Nr]
% 变换到频域
RX_FD = zeros(Nsc,Nsym,Nr);
for nr = 1:Nr
    for i = 1:Nsym
        RX_FD(:,i,nr) = fft(RX_FD_tmp(:,i,nr),Nsc);
    end
end

H_est=zeros(Nsc,Nsym,Nr);
H_est=RX_FD./TX_FD(:,:,1);
H_est(RBNum*6+1:Nsc-RBNum*6,:,:)=0;
% figure(1);
% stem(unwrap(angle(squeeze(H_est(1,1,:)))))
% figure(2);
% stem(unwrap(angle(squeeze(H_est(:,1,1)))))
% figure(3);
% stem(unwrap(angle(squeeze(H_est(1,:,1)))))

%[theta_est] = DoA(H_est,ScS,fc,0.5,1,0)
[SpeedDistMatrix,d,v] = tdfft(H_est(:,:,2),  SlotNum_zp, fNum_zp, 1,ScS,14/ScS,fc,1,Ncp,Nsym);

%% CFAR---求信噪比
N_tc=4;N_gc=6;N_tr=4;N_gr=6;CFAR_th=16;% CFAR门限/dB
Power_sum=abs(SpeedDistMatrix).^2;
% % 先取第一根天线
% H_tmp=H_est(:,:,1);
% H_fft2=fft2(H_tmp,fNum_zp,SlotNum_zp);
% Power_sum=10*log10(abs(H_fft2).^2);
% % 合并天线
% for ant=1:Nr
%     Power_sum=Power_sum+abs(H_est(:,:,ant)).^2;
% end
% Power_sum=Power_sum/Nr;
JudgeMatrix=zeros(fNum_zp+2*(N_tc+N_gc),SlotNum_zp+2*(N_gr+N_tr));
JudgeMatrix(N_tc+N_gc+1:fNum_zp+N_tc+N_gc,N_gr+N_tr+1:SlotNum_zp+N_gr+N_tr)=Power_sum;
% 
SINR_CFAR=zeros(fNum_zp,SlotNum_zp);
CFAR_Output=zeros(fNum_zp,SlotNum_zp);
Ref_num =(2*(N_tc+N_gc)+1)*(2*(N_tr+N_gr)+1)-(2*N_gc+1)*(2*N_gr+1);%参考单元个数
for i = N_tc+N_gc+1:fNum_zp+N_tc+N_gc
  for j= N_tr+N_gr+1:N_tr+N_gr+SlotNum_zp
        rcMatrix = JudgeMatrix(i-(N_tc+N_gc):i+(N_tc+N_gc),j-(N_tr+N_gr):j+(N_tr+N_gr));% 参考单元矩阵
        %去除保护单元
        rcMatrix(N_tc+1:N_tc+1+2*N_gc,N_tr+1:N_tr+1+2*N_gr) = 0;
        %求参考单元均值
        P_noise = sum(sum(rcMatrix))/Ref_num;
        SINR_CFAR(i-(N_tc+N_gc),j-(N_tr+N_gr)) = 10*log10(JudgeMatrix(i,j)/P_noise);
%         if SINR_CFAR(i-(N_tc+N_gc),j-(N_tr+N_gr)) >= CFAR_TH
%              CFAR_Output(i-(N_tc+N_gc),j-(N_tr+N_gr))= 1;
%         else
%              CFAR_Output(i-(N_tc+N_gc),j-(N_tr+N_gr))= 0;
%         end
   end
end
% figure(1);
% mesh(abs(SpeedDistMatrix));
% figure(2);
% mesh(abs(SINR_CFAR));
[~,index]=max(SINR_CFAR(:));
[MaxIndexDist, MaxIndexVel] = ind2sub(size(SINR_CFAR), index);
    Dist_estim=(MaxIndexDist-1)*c/(2*ScS*4096);
    Velo_estim=(MaxIndexVel-SlotNum_zp/2-1)*Nsym/SlotNum_zp*c/(2*TPRI*Nsym*fc);
H_angle=squeeze(H_est(MaxIndexDist,MaxIndexVel,:));
    % 扫描到达角
    Rd = (H_angle*H_angle');          
    theta_grid = linspace(-90,90,n_angle);
    P_Scan = zeros(length(theta_grid),1);
    for i_theta = 1:length(theta_grid)
        theta = theta_grid(i_theta);
        a_x = exp(-1j*pi*(0:Nr-1)'*sind(theta));
        P_Scan(i_theta) = abs(a_x'*Rd*a_x);
    end
    [~,idx] = max(P_Scan);
    theta_est = theta_grid(idx);
    % 转换为与x轴正向的夹角
    if theta_est<0
        theta_est=abs(theta_est)+90;
    else
        theta_est=90-theta_est;
    end
end